﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AbingtonBooks.Pages.Cart
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Customer"] != null)
            {
                if (!IsPostBack)
                {
                    ShoppingCart cart = ShoppingCart.getShoppingCart();
                    gridItems.DataSource = cart.Items;
                    gridItems.DataBind();
                    lblTotal.Text = (cart.Total > 0) ? "Total: " + "$" + cart.Total.ToString() : "Your Cart is empty";
                    if (cart.Items.Count == 0) {
                        btnCheckout.Visible = false;
                    }
                }
            }
            else {
                Response.Redirect("~/Pages/Login");
            }

        }

        protected void remove_Command(object sender, CommandEventArgs e)
        {
            ShoppingCart cart = ShoppingCart.getShoppingCart();
            using (dbAbingtonBooks context = new dbAbingtonBooks())
            {
                int bookId = Convert.ToInt32(e.CommandArgument);
                Book myBook = context.Books.Where(i => i.BookId == bookId).First();
                cart.removeItemFromCart(myBook);
                gridItems.DataSource = cart.Items;
                gridItems.DataBind();
            }
            lblTotal.Text = (cart.Total > 0) ? "Total: " + "$" + cart.Total.ToString() : "Your Cart is empty";
            if (cart.Items.Count == 0) {
                btnCheckout.Visible = false;
            }
        }

        
    }
}